// ----------------------------------
// RSDK Project: Sonic 1/Sonic 2
// Script Description: Fall Platform Object
// Script Author: Christian Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

// Aliases
private alias 41 : TYPE_FALLPLATFORM

// Function declarations
reserve function FallPlatform_DebugDraw
reserve function FallPlatform_DebugSpawn

// Static Values

// Tables

function FallPlatform_DebugDraw
	DrawSprite(0)
end function


function FallPlatform_DebugSpawn
	CreateTempObject(TypeName[Fall Platform], 0, object.xpos, object.ypos)
	object[tempObjectPos].value3 = object[tempObjectPos].ypos
end function


event ObjectMain
	if object.value0 == true
		if object.value2 == 0
			object.priority = PRIORITY_ACTIVE
			object.value2 = 30
		end if
		if object.value4 < 64
			object.value4 += 4
		end if
	else
		if object.value4 > 0
			object.value4 -= 4
		end if
	end if
	temp0 = object.ypos
	temp0 &= -0x10000
	switch object.state
	case 0
		temp1 = object.value4
		temp1 <<= 1
		Sin(temp2, temp1)
		temp2 <<= 9
		object.ypos = object.value3
		object.ypos += temp2
		if object.propertyValue == 0
			if object.value2 != 0
				object.value2--
				if object.value2 == 0
					object.state++
					object.value2 = 32
				end if
			end if
		end if
		break
	case 1
		object.value2--
		if object.value2 == 0
			object.state++
			foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
				BoxCollisionTest(C_PLATFORM, object.entityPos, -32, -8, 32, 0, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
				if checkResult == true
					object[currentPlayer].ypos += object.yvel
					object[currentPlayer].yvel = object.yvel
					object[currentPlayer].yvel -= 0x3800
					object[currentPlayer].gravity = GRAVITY_AIR
				end if
			next
		end if
	case 2
		object.ypos += object.yvel
		object.yvel += 0x3800
		if object.outOfBounds == true
			object.ypos = object.value3
			object.yvel = 0
			object.value2 = 0
			object.state++
		end if
		break
	case 3
		if object.outOfBounds == true
			object.state = 0
			object.priority = PRIORITY_ACTIVE_BOUNDS
		end if
		break
	end switch
	object.value1 = object.ypos
	object.value1 &= -0x10000
	object.value1 -= temp0
	object.value0 = false
	if object.state < 2
		temp1 = object.ypos
		object.ypos = temp0
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			BoxCollisionTest(C_PLATFORM, object.entityPos, -32, -8, 32, 0, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
			if checkResult == true
				object.value0 = true
				object[currentPlayer].ypos += object.value1
			end if
		next
		object.ypos = temp1
	end if
end event


event ObjectDraw
	if object.state < 3
		DrawSprite(0)
	end if
end event


event ObjectStartup
	CheckCurrentStageFolder("WIZ")
	if checkResult == true
		LoadSpriteSheet("WIZ/Objects.gif")
		SpriteFrame(-32, -12, 64, 32, 447, 479)
	else
		LoadSpriteSheet("MBZ/Objects.gif")
		SpriteFrame(-32, -8, 64, 32, 1, 402)
	end if
	foreach (TypeName[Fall Platform], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].value3 = object[arrayPos0].ypos
	next
	SetTableValue(TypeName[Fall Platform], DebugMode_ObjCount, DebugMode_TypesTable)
	SetTableValue(FallPlatform_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
	SetTableValue(FallPlatform_DebugSpawn, DebugMode_ObjCount, DebugMode_SpawnTable)
	DebugMode_ObjCount++
end event

event RSDKDraw
	DrawSprite(0)
end event

event RSDKLoad
	LoadSpriteSheet("Global/Display.gif")
	SpriteFrame(-16, -16, 32, 32, 1, 143)
end event
